﻿using System.Diagnostics;
using ComponentSystem;
using Cysharp.Threading.Tasks;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;

namespace LubanSystem
{
    /// <summary>
    /// Luban服务实体
    /// </summary>
    public partial class LubanService_Entity : EntityCSharp
    {
        /// <summary>
        /// 单例实例
        /// </summary>
        public static LubanService_Entity Instance = null;
        /// <summary>
        /// 读写区域路径模式
        /// </summary>
        protected PersistentData_Com persistentData_Com => this.GetComponent<PersistentData_Com>();
        /// <summary>
        /// Res内部只读区域路径
        /// </summary>
        protected Resources_Com resources_Com => this.GetComponent<Resources_Com>();
        /// <summary>
        /// stream内部只读区域路径
        /// </summary>
        protected StreamingAssets_Com streamingAssets_Com => this.GetComponent<StreamingAssets_Com>();
    }

    public partial class LubanService_Entity
    {
        /// <summary>
        /// Awake初始化
        /// </summary>
        protected override void Awake()
        {
            if (Instance != null) return;

            Instance = this;

            base.Awake();
            this.AddComponent<PersistentData_Com>();
            this.AddComponent<Resources_Com>();
            this.AddComponent<StreamingAssets_Com>();
        }

        #region LoadConfig To Json

        /// <summary>
        /// 从Resources加载配置文件 并且转成Json字符串
        /// </summary>
        /// <param name="path">配置文件路径</param>
        /// <returns>Json字符串</returns>
        public string LoadConfig_ToJson_Resources(string path)
        {
            return resources_Com.LoadConfigs(path);
        }

        /// <summary>
        /// 从PersistentData加载配置文件 并且转成Json字符串
        /// </summary>
        /// <param name="path">配置文件路径</param>
        /// <returns>Json字符串</returns>
        public string LoadConfig_ToJson_PersistentData(string path)
        {
            return persistentData_Com.LoadConfigs(path);
        }

        /// <summary>
        /// 从StreamingAssets加载配置文件 并且转成Json字符串
        /// </summary>
        /// <param name="path">配置文件路径</param>
        /// <returns>Json字符串</returns>
        public string LoadConfig_ToJson_StreamingAssets(string path)
        {
            return streamingAssets_Com.LoadConfigs(path);
        }

        #endregion

        #region LoadConfig To JArray

        /// <summary>
        /// 从Resources加载所有配置文件 并且转成JArray
        /// </summary>
        /// <param name="path">配置文件路径</param>
        /// <returns>JArray</returns>
        public JArray LoadConfig_ToJArray_Resources(string path)
        {
            string data = LoadConfig_ToJson_Resources(path);
            
            if (string.IsNullOrEmpty(data))
            {
                UnityEngine.Debug.LogError($"LubanSystem LoadConfig_ToJArray_Resources 读取失败: {path}");
                return null;
            }

            return JsonConvert.DeserializeObject<JArray>(data);
        }

        /// <summary>
        /// 从PersistentData加载所有配置文件 并且转成JArray
        /// </summary>
        /// <param name="path">配置文件路径</param>
        /// <returns>JArray</returns>
        public JArray LoadConfig_ToJArray_PersistentData(string path)
        {
            string data = LoadConfig_ToJson_PersistentData(path);

            if (string.IsNullOrEmpty(data))
            {
                UnityEngine.Debug.LogError($"LubanSystem LoadConfig_ToJArray_PersistentData 读取失败: {path}");
                return null;
            }

            return JsonConvert.DeserializeObject<JArray>(data);
        }

        /// <summary>
        /// 从StreamingAssets加载所有配置文件 并且转成JArray
        /// </summary>
        /// <param name="path">配置文件路径</param>
        /// <returns>JArray</returns>
        public JArray LoadConfig_ToJArray_StreamingAssets(string path)
        {
            string data = LoadConfig_ToJson_StreamingAssets(path);

            if (string.IsNullOrEmpty(data))
            {
                UnityEngine.Debug.LogError($"LubanSystem LoadConfig_ToJArray_StreamingAssets 读取失败: {path}");
                return null;
            }

            return JsonConvert.DeserializeObject<JArray>(data);
        }
        
        #endregion

    }
}